package com.tl.khadmin.controller;
 
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.tl.khadmin.bean.ProductBrand;
import com.tl.khadmin.bean.ProductType;
import com.tl.khadmin.bean.dto.ProductTypeTreeDto;
import com.tl.khadmin.service.ProductTypeIService;
import com.tl.khadmin.util.CommonUtil;

@RestController
@RequestMapping(value = "/producttype")
public class ProductTypeController {
	
	@Resource
	private ProductTypeIService productTypeService; 
	
	/**
	 * @param param level：树级别 
	 * @param param parentId：父级id
	 * @param param typeName：分类名称（模糊查询）
	 * @return
	 */
	@RequestMapping(value="/list") 
	public Map<String, List<ProductType>> listProductType(@RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("result", productTypeService.listProductType(param));
	}
	
	/**
	 * 获取商品类型树形结构数据
	 * @param param
	 * @return
	 */
	@RequestMapping(value="/tree") 
	public Map<String, List<ProductTypeTreeDto>> queryProductTypeTree(@RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("result", productTypeService.queryProductTypeTree(param));
	}
	
	@RequestMapping(value = "/save")
	@ResponseBody
	public String saveProductType(HttpSession session, @Valid ProductType productType, BindingResult br) {
		return productTypeService.saveProductType(productType);
	}

	@RequestMapping(value = "/update")
	@ResponseBody
	public String updateProductType( @Valid ProductType productType, BindingResult br) {
		return productTypeService.updateProductType(productType);
	}
	
	@RequestMapping(value = "/deletebyid")
	@ResponseBody
	public String deleteProductTypeById(HttpSession session,Integer id) {
		if(id == null){
			return CommonUtil.returnMapResultByRows(0,"删除成功！","参数错误");
		}
		return productTypeService.deleteProductTypeById(session,id); 
	} 
	
}
